package com.kuaimacode.kframework.admin.services.category;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.mybatis.mapper.category.AgentServMapper;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/03/22
 */
@Service
public class  AgentServService extends ServiceImpl<AgentServMapper, AgentServ> {

	@Autowired
	private DictService dictService;


    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<AgentServ> findAll(int pageNum, int pageSize,String code) {
    		PageHelper.startPage(pageNum, pageSize);
    		QueryWrapper queryWrapper = new QueryWrapper();
    		queryWrapper.eq("agent_id", "000000");
    		queryWrapper.eq("service_class", code);
    		queryWrapper.orderByAsc("service_class","service_code");
    		List<AgentServ> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<AgentServ>(datas);
    	}

    	@Transactional
    	public String saveService(AgentServ agentService){
			String serviceClass = agentService.getServiceClass();
			//校验名称是否存在
			String serviceName = agentService.getServiceName();
			if(serviceName==null ||"".equals(serviceName.trim())) return "二级分类名称不能为空";
			if(dictService.checkExist(Constant.SERVICE_CLASS_DICT_NAME+"_"+agentService.getServiceClass(),serviceName,0l)) return "该二级分类名称已经存在";
			int serviceCode = dictService.getMaxCode(Constant.SERVICE_CLASS_DICT_NAME+"_"+serviceClass);
			Dict dict = new Dict();
			dict.setStatus((byte)Constant.DictStatusEnum.NORMAL.getCode().intValue());

			//查询父字典项id
			Dict p_dict = dictService.getDictByNameAndCode(Constant.SERVICE_CLASS_DICT_NAME,serviceClass);
			dict.setParentId(p_dict.getId());
			dict.setCode((serviceCode+1)+"");
			dict.setValue(serviceName);
			dict.setName(Constant.SERVICE_CLASS_DICT_NAME+"_"+serviceClass);
			dict.setTitle(p_dict.getValue());
			dict.setType((byte)2);
			dict.setLevel(2);
			dict.setCreateAt(new Date());
			dict.setCreateBy(SessionUtil.getUserId());
			dictService.save(dict);

			//保存agentService 表
			agentService.setServiceCode((serviceCode+1)+"");
			agentService.setAgentId(Constant.DEFAULT_AGENT_ID);
			agentService.setCreateAt(new Date());
			agentService.setCreateBy(SessionUtil.getUserId());
			agentService.setDeleteStatus(Constant.DeleteStateEnum.NORMAL.getCode());
			baseMapper.insert(agentService);

    		return "success";
		}
		//更新service
		@Transactional
		public String updateService(AgentServ agentService){

			String serviceClass = agentService.getServiceClass();

			String serviceCode = agentService.getServiceCode();
			String serviceName = agentService.getServiceName();
			//查询该字典项
			Dict dict = dictService.getDictByNameAndCode(Constant.SERVICE_CLASS_DICT_NAME+"_"+serviceClass,serviceCode);

			if(serviceName==null ||"".equals(serviceName.trim())) return "二级分类名称不能为空";
			if(dictService.checkExist(Constant.SERVICE_CLASS_DICT_NAME,serviceName,dict.getId())) return "该二级分类名称已经存在";

			//更新字典表
			dict.setValue(serviceName);
			dictService.updateById(dict);
			//更新服务表
			agentService.setUpdateAt(new Date());
			agentService.setUpdateBy(SessionUtil.getUserId());
			baseMapper.updateById(agentService);


			return "success";
		}

		public List<AgentServ> findAgetnServByAgentId(String agentId){
    		QueryWrapper<AgentServ> queryWrapper = new QueryWrapper<>();
    		queryWrapper.eq("agent_id",agentId);
    		return baseMapper.selectList(queryWrapper);
		}





}

